﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using cecbData;
using cecbClasses.Data_Adapters.ImpactAndActivity;
using cecbClasses.Data_Adapters;
using System.Windows.Media.Animation;

namespace CECB.Activity
{
    /// <summary>
    /// Interaction logic for Project_Activity_Update_Window.xaml
    /// </summary>
    public partial class Project_Activity_Update_Window : Window
    {
        public Project_Activity_Update_Window()
        {
            InitializeComponent();
            init();
        }

        ProjectActivities proAct;

        private void init()
        {
             
            cecbClasses.Data_Adapters.Slider slid = new cecbClasses.Data_Adapters.Slider();

            ProjectAutocompleteTextBox.ItemsSource = slid.getProjectDetails();
            textBoxActivitySearch.IsEnabled = false;

            
        }


        private void textBoxActivitySearch_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (textBoxActivitySearch.Text.Length > 0)
            {

                listBoxActivities.ItemsSource = new cecbClasses.Data_Adapters.ImpactAndActivity.Func_Activities().getProjectActivityDetailsByName(textBoxActivitySearch.Text.Trim(), proj);
                (FindResource("drop") as Storyboard).Begin(this);
            }
        }

        private void CloseButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (isProjectSelected)
                {

                    if (textBoxActivityUpdate.Text.ToString().Trim().Length > 0)
                    {

                        if (new cecbClasses.Data_Adapters.ImpactAndActivity.Func_Activities().checkForDeleteIsEmailsSendByProject(proAct))
                        {
                              if (MessageBox.Show("You are about to delete a activity from  ' " + ProjectAutocompleteTextBox.Text + "' project. Confirm to continue?", "Confirmation", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
                            {

                                if (new cecbClasses.Data_Adapters.ImpactAndActivity.Func_Activities().deleteProjectActivity(proAct, false))
                                {
                                    MessageBox.Show("Activity Deleted Successfully", "Message", MessageBoxButton.OK, MessageBoxImage.Information);
                                    refreshActivityList();

                                }
                                else
                                {
                                    if (MessageBox.Show("You cannot delete this activity directly. Values are added to matrix using this activity. If you delete this activity, matrix values will be lost. Do you need to continue? ", "Confirmation", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
                                    {

                                        if (new cecbClasses.Data_Adapters.ImpactAndActivity.Func_Activities().deleteProjectActivity(proAct, true))
                                            MessageBox.Show("Activity Deleted Successfully", "Message", MessageBoxButton.OK, MessageBoxImage.Information);

                                        refreshActivityList();
                                    }
                                }
                            }
                        }
                        else { MessageBox.Show("You cannot delete this activity. Emails have been sent to consultants using this activity", "Message", MessageBoxButton.OK, MessageBoxImage.Error); }
                    }
                    else
                        MessageBox.Show("Select An Activity ", "Message", MessageBoxButton.OK, MessageBoxImage.Error);

                }
                else
                    MessageBox.Show("Select A Project", "Message", MessageBoxButton.OK, MessageBoxImage.Error);


            }
            catch (Exception)
            {
                MessageBox.Show("Select A Project", "Message", MessageBoxButton.OK, MessageBoxImage.Error);

            }


        }

        private void SaveButton_Click(object sender, RoutedEventArgs e)
        {
            


            //cecbClasses.Data_Adapters.ImpactAndActivity.Func_Activities activity = new cecbClasses.Data_Adapters.ImpactAndActivity.Func_Activities();

            //if (textBoxActivityUpdate.Text == "")
            //{
            //    MessageBox.Show("Nothing has been modified", "Error", MessageBoxButton.OK, MessageBoxImage.Error);

            //}
            //else
            //{
            //    if (activity.updateActivity(_actId, textBoxActivityUpdate.Text.ToString(), comboBoxComponents.SelectedValue.ToString()))
            //    {
            //        MessageBox.Show("Project Impact Updated Successfully", "Message", MessageBoxButton.OK, MessageBoxImage.Information);
            //        textBoxActivityUpdate.Clear();


            //        listBoxActivities.ItemsSource = new cecbClasses.Data_Adapters.ImpactAndActivity.Func_Activities().getAllActivities();
            //        //int x = comboBoxComponents.SelectedIndex;
            //        //comboBoxComponents.SelectedIndex = -1;//
            //        //comboBoxComponents.SelectedIndex = x; // -1;
            //        //listBoxActivities.Visibility = System.Windows.Visibility.Hidden;
            //    }
            //    else
            //    {
            //        MessageBox.Show("Project Impact Updation failed", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            //    }

            //}

        }

        private void NewButton_Click(object sender, RoutedEventArgs e)
        {

        }

        string _actId = "";
        private void listBoxActivities_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
             try
            {
                if (listBoxActivities.SelectedIndex > -1)
                {
                    try
                    { 
                        proAct = (cecbData.ProjectActivities)listBoxActivities.SelectedItem;
                        _actId = proAct.actv_reference.ToString();
                        textBoxActivityUpdate.Text = new cecbClasses.Data_Adapters.ImpactAndActivity.Func_Activities().getActivityNameById(proAct.actv_reference.ToString());
                    }
                    catch (Exception)
                    {

                        MessageBox.Show( "Select a Project","Message", MessageBoxButton.OK, MessageBoxImage.Error);
                    }
                }
            }
            catch (Exception)
            {

            }
             
        }

        Projects proj;
        Boolean isProjectSelected;
        private void ProjectAutocomplete_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

            if (((AutoCompleteBox)sender).SelectedItem != null)
            {

                cecbClasses.Data_Adapters.Slider slid = new cecbClasses.Data_Adapters.Slider();
                proj = (Projects)ProjectAutocompleteTextBox.SelectedItem;
                ProjectDetailsWrapPanel.DataContext = slid.getProjectDetailsByRef(proj.proj_reference);
                refreshActivityList();
                textBoxActivitySearch.IsEnabled = true;
                isProjectSelected = true;
                textBoxActivityUpdate.Clear();
                labelProjectCat.Content = proj.Categories.cat_name;
                labelProjectName.Content = proj.proj_name;
                labelProjectReference.Content = proj.proj_team;
            }
            else
            {
                isProjectSelected = false;
                textBoxActivitySearch.IsEnabled = false;
                labelProjectCat.Content = "";
                labelProjectName.Content = "";
                labelProjectReference.Content = "";
                listBoxActivities.ItemsSource = null;
                listBoxActivities.Items.Clear();
            }
        }

        private void refreshActivityList() {

            listBoxActivities.ItemsSource = from c in new DSSEntities().ProjectActivities
                                            where c.proj_reference.Equals(proj.proj_reference)
                                            select c;
            (FindResource("drop") as Storyboard).Begin(this);
        }

        private void comboBoxComponents_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

         

        }

        private void Image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.Close();
        }

        private void Image_ImageFailed(object sender, ExceptionRoutedEventArgs e)
        {

        }

        private void comboBoxComponents_MouseEnter(object sender, MouseEventArgs e)
        {
            //if (ProjectAutocompleteTextBox.Text == null)
            //    MessageBox.Show("Select a project");

        }
    }
}
